package net.wanglu.www.zzz.rule.frag;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.trees.TreeGraphNode;
import edu.stanford.nlp.trees.TypedDependency;

public class InfinitivePhraseFragments extends _FragmentsRule {

	Pattern infinitivePhrase = Pattern.compile("^(\\bto/TO \\w+/VB\\b)");

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("可以给不定式残缺句添加主语和谓语, 使之成为一个独立的句子,表达出完整的意思. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: Only to watch in dismay as Dr.Frazier poured her chemistry experiment into the sink.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: Amber watched in dismay as Dr.Frazier poured her chemistry experiment into the sink.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("还可以把不定式残缺句放在一个独立的句子的句首或句尾, 成为句子的一部分. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: To catch butterflies for her biology project.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: Jossie enlisted the help of several spiders to catch butterflies for her biology project.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: To break a piece of plywood with his bare hands.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: To break a piece of plywood with his bare hands, Daniel followed his karate teacher&#39;s advice and focused power.");
		sb.append("</p>");
		return sb.toString();
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		String sentence = (String) preprocessData.get("sentence");
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");

		Collection<TypedDependency> roots = GrammaticalStructure.getRoots((Collection<TypedDependency>) tdl);
		TreeGraphNode rootDep = roots.iterator().next().dep();
		
		if ( "VB".equals(rootDep.label().tag())) {
			boolean to = false, nsubj = false;
			for( Iterator<?> itr = tdl.iterator(); itr.hasNext();) {
				TypedDependency TD = (TypedDependency) itr.next();
				if ( "aux".equals(TD.reln().getShortName())) {
					if ( TD.gov() == rootDep && "TO".equals(TD.dep().label().tag())) {
						to = true;
					}
				}
				if (  "nsubj".equals(TD.reln().getShortName())) {
					if ( TD.gov() == rootDep) {
						nsubj = true;
					}
				}
			}
			
			if ( to && !nsubj) {
				BadEgg egg = new BadEgg();
				egg.setViolatedRule(this);
				egg.whereIsWrong = sentence;
				return egg;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("不定式残缺句是以动词不定式开头. 虽然有很多单词与动词不定式构成不定式短语, 但不定式短语仍然不是一个独立的句子, 无法表达一个完整的意思.");
		sb.append("</p>");
		return sb.toString();
	}
}
